<template>
  <div class="login">
    <div class="login_box">
      <h2>系统登陆</h2>
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width
        class="login_form"
      >
        <!-- prop:对应验证规则 -->
        <el-form-item label="用户名" prop="account">
          <el-input v-model="loginForm.account" prefix-icon="iconfont icon-geren"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
            :suffix-icon="isOpen ? 'iconfont icon-yanjing1' : 'iconfont icon-yanjing'"
            :type="isOpen ? 'text' : 'password'"
            @click.native="changeOpen($event)"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { checkLogin } from '@/api/account.js'
export default {
  name: 'Login',
  props: {},
  data() {
    return {
      loginForm: {
        account: 'admin',
        // admin
        password: ''
      },
      loginFormRules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
      isOpen: false
    }
  },
  mounted() { },
  methods: {
    changeOpen($event) {
      // console.log($event.target.tagName)
      // click.native :click点击不生效
      if ($event.target.tagName === 'I') {
        this.isOpen = !this.isOpen
      }
    },
    resetLoginFrom() {
      // console.log(this.$refs.loginFormRef)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // 登录成功valid:true,返回promise对象
        if (!valid) return 0
        const { code, token, role, menus } = await checkLogin(this.loginForm)
        console.log(code, token, role, menus)
        if (code === 0) {
          window.sessionStorage.setItem('role', role)
          window.sessionStorage.setItem('token', token)
          window.sessionStorage.setItem('menus', JSON.stringify(menus))
          window.sessionStorage.setItem('user', this.loginForm.account)
          this.$router.push('/layout')
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  position: relative;
  color: #fff;
}
.login_box {
  width: 450px;
  /* height: 300px; */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login_box h2 {
  display: block;
  width: 100%;
  line-height: 40px;
  text-align: center;
  /* color: #fff; */
}
.login_form {
  width: 100%;
  height: 300px;
}
.yanjing {
  float: right;
  margin-top: -25px;
  z-index: 999;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
.login >>> .el-input__inner {
  color: #fff;
  background-color: #283443;
  outline: none;
  border-radius: 5px;
  border-color: #67707c;
}
.login >>> .icon-yanjing:hover {
  cursor: pointer;
}
</style>
